/*
 * crash.c
 *
 *  Created on: 2011-11-2
 *      Author: Dspark
 */
#include <stdio.h>
#include <malloc.h>
#include <math.h>

typedef struct SList
{
	int value;
	struct SList* next;
}list;

typedef struct SDList
{
	int value;
	struct SDList* next;
	int size;
	list* head;
	list* tail;
}group;

typedef struct SNumber
{
	int number;
	group* head;
	group* tail;
	int total;
}CNumber;

/*
 * 功能：给因子连表增加一个因子
 * 系group_addNode的底层调用
*/
list* util_list_addNode(list *node,int value)
{
	list* temp = (list *)malloc(sizeof(list));
	temp->value = value;
	temp->next = NULL;
	node->next = temp;
	return temp;
}

list* util_list_creat(int value)
{
	list* temp = (list*)malloc(sizeof(list));
	temp->value = value;
	temp->next = NULL;
	return temp;
}

/*
 * 功能：给一个group增加一个因子，每个group存放一组（被分解的）因子
 */
void util_group_addNode(group* group,int value)
{
	if(group->tail==NULL)
	{
		group->tail = util_list_creat(value);
		group->head = group->tail;
		group->size = 1;
	}
	else
	{
		group->tail = util_list_addNode(group->tail,value);
		group->size++;
	}
}

/*功能：创建一组group*/
group* util_group_create(int value)
{
	group* temp = (group*)malloc(sizeof(group));
	temp->head = util_list_creat(value);
	temp->tail = temp->head;
	temp->size = 1;
	temp->value = 0;
	temp->next = NULL;
	return temp;
}
/*
 * 功能：增加一组因子组(group),因子组包含了一组乘积为number->number的数
 */
void util_CNumber_addGroup(CNumber *number, group* group)
{
	if(number->tail!=NULL)
	{
		number->tail->next = group;
		group->next = NULL;
		number->tail = group;
		number->total+=group->size;
	}else
	{
		for(number->tail=group,number->head=group; group; group=group->next)
		{
			number->total += group->size;
			number->tail = group;
		}
	}
}
int* util_CNumber_groups2Array(CNumber* number)
{
		int *array;
		group* temp;
		list* divisors;
		int index = 0;
		array = (int *)calloc(number->total,sizeof(int));
		for(temp=number->head;temp;temp=temp->next)
		{
			for(divisors=temp->head;	divisors;	divisors=divisors->next, index++)
				array[index] = divisors->value;
		}
		return array;
}

void util_CNumber_workoutGroups(CNumber* number)
{
	if(number->number == 2)
			return;
	int index, i, size;
	size = (int)pow((double)(number->number),0.5);
	int* array = (int *)calloc(size,sizeof(int));
	for(i=2, index=0; i<=size; i++)
	{
		if((number->number)%i==0)
		{
			int j,flag = 1;
			for(j=index; j >= 0; j--)
			{
				if(array[j]==i)
				{
					flag=0;
					break;
				}
			}

			if(flag)
			{
				index++;
				*(array+index)= i;
				group* temp = util_group_create(i);
				if((number->number)/i<=100)
				{
					i++;
					*(array+index)=(number->number)/i;
					util_group_addNode(temp,*(array+index));
					util_CNumber_addGroup(number,temp);
				}
				else
				{
					int a = (number->number)/i;
					for(i++;i<size && a >100;i++)
					{
						if(a%i==0)
						{
							index++;
							*(array+index)= i;
							util_group_addNode(temp,i);
							a = a/i;
						}
					}
					index++;
					*(array+index)=a;
					util_group_addNode(temp,a);
					util_CNumber_addGroup(number,temp);
				}
			}
		}
	}

	if(number->number<=100)
		{
			group* temp = util_group_create(number->number);
			util_CNumber_addGroup(number,temp);
		}
}

CNumber* util_CNumber_create()
{
	CNumber* c = (CNumber*)malloc(sizeof(CNumber));
	c->number = 0;
	c->head = NULL;
	c->tail = NULL;
	c->total = 0;
	return c;
}

/*
 * 看target中有没有source的元素，有则返回0
 */
int util_group_compare(group* source,group* target)
{
	list* sl,*tl;
	sl = source->head, tl = target->head;
	while(sl && tl)
	{
		if(sl->value == tl->value)
			return 0;
		else if(sl->value > tl->value)
		{
			tl = tl->next;
		}
		else
		{
			sl = sl->next;
		}
	}
	return 1;
}

/*
 * 将一组因子与因子组群比较，若能灭掉因子组群中的所有因子
 ＊则返回0，即挑战成功
 */
int util_group_compareToGroups(group* source,group* groups)
{
	group* temp;
	for(temp= groups;	temp;	temp=temp->next)
	{
		if(util_group_compare(source,temp))
			return 1; /*找到了一个不包含source任一元素的group，挑战失败*/
	}
	return 0;
}


int util_CNumber_compare(CNumber* a, CNumber* b)
{
	group* bh;
	for(bh = b->head;	bh;	bh=bh->next)
	{
		if(util_group_compareToGroups(bh,a->head))
			return 1;	/* 挑战者失败 */
	}
	return 0;
}

void util_CNumber_clear(CNumber* a)
{
	a->head = NULL;
	a->number = 0;
	a->tail = NULL;
	a->total = 0;
}

int main(int argc, char **argv)
{

	CNumber* a;
	CNumber* b;
	a = util_CNumber_create();
	b = util_CNumber_create();
	while((scanf("%d %d",&(a->number),&(b->number )))!=EOF)
	{
		util_CNumber_workoutGroups(a);
		if(a->total==0)
		{
			printf("%d\n",b->number);
			return 0;
		}
		util_CNumber_workoutGroups(b);
		if(util_CNumber_compare(a,b))
			printf("%d\n",a->number);
		else
			printf("%d\n",a->number);
		util_CNumber_clear(a);
		util_CNumber_clear(b);
	}
	return 0;
}




